from django.db import models
import datetime
from django.contrib.auth.models import User
from TuiGuang.models import tg_qudao



class KeHu(models.Model):
    username = models.CharField(verbose_name='客户', max_length=22, default='')
    name = models.CharField(verbose_name='微信联系人', max_length=22, default='',blank=True,null=True)
    tel = models.CharField(verbose_name='联系电话', max_length=12, default='')
    wechat=models.CharField(verbose_name='微信号', max_length=22,blank=True,null=True)
    qq=models.CharField(verbose_name='QQ号', max_length=22, default='',blank=True,null=True)
    beizhu = models.CharField(verbose_name='客户备注', max_length=255, default='',null=True)
    zixuntime = models.DateTimeField(verbose_name='客户咨询时间', default=datetime.datetime.now,null=True,)
    addtime = models.DateTimeField(verbose_name='客户新建时间', auto_now_add=True, null=True)
    admin_user = models.ForeignKey(User, verbose_name='所属用户', on_delete=models.CASCADE, null=True)
    add_admin_user = models.ForeignKey(User, verbose_name='创建用户', on_delete=models.CASCADE, null=True,blank=True,related_name="add_admin_user")
    tg_qudao = models.ForeignKey(tg_qudao, verbose_name='所属渠道', on_delete=models.CASCADE, null=True)
    # 添加反向关联
    kehu_zhaungtai = models.OneToOneField('KeHu_zhaungtai', verbose_name='客户状态', on_delete=models.CASCADE, null=True,related_name='kehu_related')
    gonghai_choices = [(0, '未标注'),(1, '已标注'),]
    if_gonghai = models.SmallIntegerField(verbose_name='是否有公海', default='0', choices = gonghai_choices, null=True)


    def __str__(self):
        return self.username

    class Meta:
        verbose_name = '客户'
        verbose_name_plural = '客户管理'

class KeHuzilao(models.Model):
    """客户资料"""
    kehu = models.OneToOneField(KeHu, on_delete=models.CASCADE,null=True,verbose_name='关联客户')
    fujian=models.FileField(verbose_name='附件', upload_to='fujian/', null=True,blank=True)
    huji_choices = [
        (0, '未知'),
        (1, '广东.广州'),
        (2, '广东.佛山'),
        (3, '异地'),
    ]
    hujileixing = models.SmallIntegerField(verbose_name='户籍类型', default='0', choices=huji_choices)
    hujizhenghao = models.CharField(verbose_name='户籍证号', max_length=20, blank=True, null=True)


    def __str__(self):
        return self.kehu.name

    class Meta:
        verbose_name = '客户资料'
        verbose_name_plural = '客户资料'



class KeHugenjinlog(models.Model):
    kehu= models.ForeignKey('KeHu', on_delete=models.CASCADE)
    genjin_choices = [
        (0, '普通'),
        (1, '重要'),
        (2, '提醒'),
    ]
    genjinleixing = models.SmallIntegerField(verbose_name='跟进类型', default='0', choices=genjin_choices)
    genjintext=models.TextField(verbose_name='跟进内容', null=True)
    genjintime = models.DateTimeField(verbose_name='跟进时间',null=True,default=datetime.datetime.now)
    genjin_user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)

    class Meta:
        verbose_name = '跟进记录'
        verbose_name_plural = '客户跟进记录'

class KeHu_zhaungtai(models.Model):
    kehu = models.OneToOneField('KeHu', on_delete=models.CASCADE, null=True, related_name='kehu_related')
    yxd_choices = [
        (0, '未知'),
        (1, '高意向'),
        (2, '中意向'),
        (3, '低意向'),
    ]
    yixiangdu = models.SmallIntegerField(verbose_name='意向度', default='0', choices=yxd_choices, null=True)
    yxx_choices = [
        (0, '未建立沟通'),
        (1, '有效'),
        (2, '无效'),
    ]
    youxiaoif = models.SmallIntegerField(verbose_name='有效性', default='0', choices=yxx_choices, null=True)
    gz_choices = [
        (1, '未标注'),
        (2, '已标注'),
    ]
    guanzhu = models.SmallIntegerField(verbose_name='星标客户', default='1', choices=gz_choices, null=True)

